package main

import (
	"fmt"
	common "projRoot/commonLib"
	"time"
)

func lock_unlock_multiple_normal(portID int, s common.LockService) {
	args := common.LockArguments{Client_portID: portID,
		Request_time: fmt.Sprintf("%d:23:59", portID)}
	count := 1
	result := common.LockState{}
	for i := 0; i < 1000000; i++ {
		args.Request_time = fmt.Sprintf("%d:23:%d", portID, count)
		count++
		(&s).Acquire(&args, &result)
		args.Request_time = fmt.Sprintf("%d:23:%d", portID, count)
		count++
		(&s).Release(&args, &result)
	}

	fmt.Printf("port %d got done: %v\n", portID, result)
}

func lock_unlock_multiple_repost(portID int, s common.LockService) {
	args := common.LockArguments{Client_portID: portID,
		Request_time: fmt.Sprintf("%d:23:59", portID)}
	result := common.LockState{}

	err := (&s).Acquire(&args, &result)
	if err != nil {
		fmt.Printf("port %d 上锁失败:%v\n", portID, err)
	}
	err = (&s).Release(&args, &result)
	if err != nil {
		fmt.Printf("port %d 解锁失败:%v\n", portID, err)
	}

	fmt.Printf("port %d got done: %v\n", portID, result)
}

func main() {
	var s common.LockService = common.LockService{}

	wait1 := make(chan bool)
	wait2 := make(chan bool)

	go func(wait chan bool, s common.LockService) {
		lock_unlock_multiple_repost(45, s)
		wait <- true
	}(wait1, s)

	go func(wait chan bool, s common.LockService) {
		lock_unlock_multiple_repost(17, s)
		wait <- true
	}(wait2, s)
	time.Sleep(time.Millisecond)
	lock_unlock_multiple_repost(12, s)
	<-wait1
	<-wait2

	fmt.Println("总操作次数:", common.Unittest_count)
	final_state := common.LockState{}
	s.Query(nil, &final_state)
	fmt.Println("锁最终状态:", final_state)

	var tmp map[string]bool
	fmt.Println(tmp == nil)
	tmp = make(map[string]bool)
	fmt.Println(tmp == nil)
}
